﻿using LibNiconico.NiconicoApi.Lives.Broadcast;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Reflection;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NicoView.Model.Service.Broadcast
{
    public enum NicoViewBroadcastPresetType
    {
        General
    }


    public class NicoViewBroadcastPreset:NicoViewObservableObject
    {
        public NicoViewBroadcastPreset()
        {
            this.Tags = new NicoViewNiconicoTagCollection();

            this.presetType = NicoViewBroadcastPresetType.General;

            this.communityId = "";

            this.primaryCategory = PrimaryCategory.None;

            this.description = "";


            this.title = "";

            this.publicStatus = NicoLivePublicStatus.All;
            this.adEnable = false;
            this.timeShiftEnabled = false;
            this.twitterEnabled = false;

            this.twitterTag = "";

        }


        #region 通知プロパティ

        private Guid presetId;

        public Guid PresetId
        {
            get
            {
                return this.presetId;
            }

            set
            {
                this.Set(() => this.PresetId, ref this.presetId, value);
            }
        }

        private string twitterTag;

        public string TwitterTag
        {
            get
            {
                return this.twitterTag;
            }

            set
            {
                this.Set(() => this.TwitterTag, ref this.twitterTag, value);
            }
        }

        private bool twitterEnabled;

        public bool TwitterEnabled
        {
            get
            {
                return this.twitterEnabled;
            }

            set
            {
                this.Set(() => this.TwitterEnabled, ref this.twitterEnabled, value);
            }
        }

        private bool timeShiftEnabled;

        public bool TimeShiftEnabled
        {
            get
            {
                return this.timeShiftEnabled;
            }

            set
            {
                this.Set(() => this.TimeShiftEnabled, ref this.timeShiftEnabled, value);
            }
        }

        private bool adEnable;

        public bool AdEnable
        {
            get
            {
                return this.adEnable;
            }

            set
            {
                this.Set(() => this.AdEnable, ref this.adEnable, value);
            }
        }

        private NicoLivePublicStatus publicStatus;


        public NicoLivePublicStatus PublicStatus
        {
            get
            {
                return this.publicStatus;
            }

            set
            {
                this.Set(() => this.PublicStatus, ref this.publicStatus, value);
            }
        }

        private string description;

        public string Description
        {
            get
            {
                return this.description;
            }

            set
            {
                this.Set(() => this.Description, ref this.description, value);
            }
        }

        private string title;

        public string Title
        {
            get
            {
                return this.title;
            }

            set
            {
                this.Set(() => this.Title, ref this.title, value);
            }
        }

        private PrimaryCategory primaryCategory;


        public PrimaryCategory PrimaryCategory
        {
            get
            {
                return this.primaryCategory;
            }

            set
            {
                this.Set(() => this.PrimaryCategory, ref this.primaryCategory, value);
            }
        }



        private bool outFace;

        public bool OutFace
        {
            get
            {
                return this.outFace;
            }

            set
            {
                this.Set(() => this.OutFace, ref this.outFace, value);
            }
        }


        private bool cruiseWait;

        public bool CruiseWait
        {
            get
            {
                return this.cruiseWait;
            }

            set
            {
                this.Set(() => this.CruiseWait, ref this.cruiseWait, value);
            }
        }


        private bool assaultWait;

        public bool AssaultWait
        {
            get
            {
                return this.assaultWait;
            }

            set
            {
                this.Set(() => this.AssaultWait, ref this.assaultWait, value);
            }
        }



        private NicoViewBroadcastPresetType presetType;

        [JsonProperty]
        [JsonConverter(typeof(StringEnumConverter))]
        public NicoViewBroadcastPresetType PresetType
        {
            get
            {
                return this.presetType;
            }

            set
            {
                this.Set(() => this.PresetType, ref this.presetType, value);
            }

        }


        private string communityId;

        public string CommunityId
        {
            get
            {
                return this.communityId;
            }

            set
            {
                this.Set(() => this.CommunityId, ref this.communityId, value);
               
            }
        }
        public NicoViewNiconicoTagCollection Tags { get; private set; }


        #endregion



       

        internal NicoLiveBroadcastCreateQueryInfo CreateAsBroadcastCreateQueryInfo(NicoLiveBroadcastableCommunityInfo comunityInfo)
        {

            if (comunityInfo.CommunityId != this.CommunityId) throw new ArgumentException();

            var queryInfo = new NicoLiveBroadcastCreateQueryInfo();

            queryInfo.PrimaryCategory = this.PrimaryCategory;


            if (this.OutFace && ! queryInfo.SecondaryCategorys.Contains(SecondaryCategory.OutFace))
            {
                queryInfo.SecondaryCategorys.Add(SecondaryCategory.OutFace);
            }

            if (this.AssaultWait && ! queryInfo.SecondaryCategorys.Contains(SecondaryCategory.AssaultWait))
            {
                queryInfo.SecondaryCategorys.Add(SecondaryCategory.AssaultWait);
            }

            if (this.CruiseWait && ! queryInfo.SecondaryCategorys.Contains(SecondaryCategory.CruiseWait))
            {
                queryInfo.SecondaryCategorys.Add(SecondaryCategory.CruiseWait);
            }


            queryInfo.Title = this.Title;
            queryInfo.Description = this.Description;
            queryInfo.PublicStatus = this.PublicStatus;

            foreach (var tag in this.Tags)
            {
                queryInfo.Tags.Add(tag.TagName, tag.IsLocked);
            }


            queryInfo.DefaultCommunity = comunityInfo;

            queryInfo.AdEnable = this.AdEnable;

            queryInfo.TimeShiftEnabled = this.TimeShiftEnabled;

            queryInfo.TwitterDisabled = !this.TwitterEnabled;


            if (this.twitterTag != "") queryInfo.TwitterTag = this.TwitterTag;

            

            return queryInfo;
        }

        

        public object Clone()
        {

            var cloneObject = (NicoViewBroadcastPreset) this.MemberwiseClone();

            cloneObject.presetId = new Guid();


            return cloneObject;
        }
    }
}
